import * as api from './api';
import {isYesNo, reportFormList} from '../Enum';
export const ERROR_CODE = 'error';
export const GET_INIT_DATA_BEGIN = 'GET_INIT_DATA_BEGIN';
export const GET_INIT_DATA_SUCCESS = 'GET_INIT_DATA_SUCCESS';
export const GET_INIT_DATA_FAIL = 'GET_INIT_DATA_FAIL';

/*==============================查询列表================================= */
export const GET_LIST_BEGIN = 'GET_LIST_BEGIN';
export const GET_LIST_SUCCESS = 'GET_LIST_SUCCESS';
export const GET_LIST_FAIL = 'GET_LIST_FAIL';
export const getListBegin = () => ({
    type: GET_LIST_BEGIN
});
const getListSuccess = (condition, data, n) => ({
    type: GET_LIST_SUCCESS,
    condition,
    data,
    n
});

export const getListFail = () => ({
    type: GET_LIST_FAIL
});

/*==============================初始化大区================================= */
export const GET_INIT_CATEGORY_DATA_BEGIN = 'GET_INIT_CATEGORY_DATA_BEGIN';
export const GET_INIT_CATEGORY_DATA_SUCCESS = 'GET_INIT_CATEGORY_DATA_SUCCESS';
export const GET_INIT_CATEGORY_DATA_FAIL = 'GET_INIT_CATEGORY_DATA_FAIL';
const getInitCategoryDataBegin = () => ({
    type: GET_INIT_CATEGORY_DATA_BEGIN
});

const getInitCategoryDataSuccess = data => ({
    type: GET_INIT_CATEGORY_DATA_SUCCESS,
    data
});
const getInitCategoryDataFail = () => ({
    type: GET_INIT_CATEGORY_DATA_FAIL
});

/*==============================初始化API================================= */
//获取初始化数据和权限
export const getInitData = () => dispatch => {
    dispatch(getInitCategoryDataBegin());
    const category = api.getList().then(res => {
        if(res.ok)
            dispatch(getInitCategoryDataSuccess(res.data));
        else
            dispatch(getInitCategoryDataFail());
        return res.ok;
    });
    return Promise.all([category]).then(values => values[0]);
};

import moment from 'moment';
const submitTimeDate = (condition = {}) => {
    const {submitTime, ...others} = condition;
    if(!submitTime || submitTime.length === 0) return condition;
    return {
        ...others,
        beginSubmitTime: moment(submitTime[0]).toISOString(),
        endSubmitTime: moment(submitTime[1]).toISOString(),
    };
};
const approveTimeDate = (condition = {}) => {
    const {approveTime, ...others} = condition;
    if(!approveTime || approveTime.length === 0) return condition;
    return {
        ...others,
        beginApproveTime: moment(approveTime[0]).toISOString(),
        endApproveTime: moment(approveTime[1]).toISOString(),
    };
};
const banlaceDateTime = (condition = {}) => {
    const {banlaceDate, ...others} = condition;
    if(!banlaceDate || banlaceDate.length === 0) return condition;
    return {
        ...others,
        beginBanlaceDate: moment(banlaceDate[0]).toISOString(),
        endBanlaceDate: moment(banlaceDate[1]).toISOString(),
    };
};
const productionDateTime = (condition = {}) => {
    const {productionDate, ...others} = condition;
    if(!productionDate || productionDate.length === 0) return condition;
    return {
        ...others,
        beginProductionDate: moment(productionDate[0]).toISOString(),
        endProductionDate: moment(productionDate[1]).toISOString(),
    };
};
const saleDateTime = (condition = {}) => {
    const {saleDate, ...others} = condition;
    if(!saleDate || saleDate.length === 0) return condition;
    return {
        ...others,
        beginSaleDate: moment(saleDate[0]).toISOString(),
        endSaleDate: moment(saleDate[1]).toISOString(),
    };
};
/*==============================查询action================================= */
//获取查询列表
export const getList = condition => dispatch => {
    dispatch(getListBegin());
    const queryCondition = submitTimeDate(approveTimeDate(banlaceDateTime(productionDateTime(saleDateTime(condition)))));
    let yearonYear = undefined;
    if(condition.yearonYear === isYesNo.是)
        yearonYear = true;
    if(condition.yearonYear === isYesNo.否)
        yearonYear = false;
    let linkRatio = undefined;
    if(condition.linkRatio === isYesNo.是)
        linkRatio = true;
    if(condition.linkRatio === isYesNo.否)
        linkRatio = false;
    if(condition.reportForm === reportFormList.索赔单据报表 && condition.yearonYear === isYesNo.否) {
        delete queryCondition.linkRatioTime;
        delete queryCondition.linkRatio;
        return api.getBillFalse({
            ...queryCondition,
            yearonYear
        }).then(res => {
            if(res.ok)
                dispatch(getListSuccess(condition, res.data));
            else
                dispatch(getListFail());
        });
    } else if(condition.reportForm === reportFormList.索赔单据报表 && condition.yearonYear === isYesNo.是) {
        delete queryCondition.beginSubmitTime;
        delete queryCondition.endSubmitTime;
        delete queryCondition.linkRatioTime;
        delete queryCondition.linkRatio;
        return api.getBillTrue({
            ...queryCondition,
            yearonYear
        }).then(res => {
            if(res.ok)
                dispatch(getListSuccess(condition, res.data));
            else
                dispatch(getListFail());
        });
    } else if(condition.reportForm === reportFormList.单据审核报表 && condition.yearonYear === isYesNo.否) {
        delete queryCondition.linkRatioTime;
        delete queryCondition.linkRatio;
        return api.getApproveFalse({
            ...queryCondition,
            yearonYear
        }).then(res => {
            if(res.ok)
                dispatch(getListSuccess(condition, res.data));
            else
                dispatch(getListFail());
        });
    } else if(condition.reportForm === reportFormList.单据审核报表 && condition.yearonYear === isYesNo.是) {
        delete queryCondition.beginSubmitTime;
        delete queryCondition.endSubmitTime;
        delete queryCondition.linkRatioTime;
        delete queryCondition.linkRatio;
        return api.getApproveTrue({
            ...queryCondition,
            yearonYear
        }).then(res => {
            if(res.ok)
                dispatch(getListSuccess(condition, res.data));
            else
                dispatch(getListFail());
        });
    } else if(condition.reportForm === reportFormList.索赔单据量 && condition.linkRatio === isYesNo.是) {
        delete queryCondition.beginSubmitTime;
        delete queryCondition.endSubmitTime;
        delete queryCondition.yearonYear;
        return api.getBillAmount({
            ...queryCondition,
            linkRatio
        }).then(res => {
            if(res.ok)
                dispatch(getListSuccess(condition, res.data));
            else
                dispatch(getListFail());
        });
    } else if((condition.reportForm === reportFormList.通过率 || condition.reportForm === reportFormList.一次通过率) && condition.linkRatio === isYesNo.是) {
        delete queryCondition.beginSubmitTime;
        delete queryCondition.endSubmitTime;
        delete queryCondition.yearonYear;
        return api.getOnePassRate({
            ...queryCondition,
            linkRatio
        }).then(res => {
            if(res.ok)
                dispatch(getListSuccess(condition, res.data));
            else
                dispatch(getListFail());
        });
    } else if((condition.reportForm === reportFormList.拒绝 || condition.reportForm === reportFormList.退回) && condition.linkRatio === isYesNo.否) {
        delete queryCondition.beginSubmitTime;
        delete queryCondition.endSubmitTime;
        delete queryCondition.yearonYear;
        return api.getRejectFalse({
            ...queryCondition,
            linkRatio
        }).then(res => {
            if(res.ok)
                dispatch(getListSuccess(condition, res.data));
            else
                dispatch(getListFail());
        });
    } else if((condition.reportForm === reportFormList.拒绝 || condition.reportForm === reportFormList.退回) && condition.linkRatio === isYesNo.是) {
        delete queryCondition.beginSubmitTime;
        delete queryCondition.endSubmitTime;
        delete queryCondition.yearonYear;
        return api.getRejectTrue({
            ...queryCondition,
            linkRatio
        }).then(res => {
            if(res.ok)
                dispatch(getListSuccess(condition, res.data));
            else
                dispatch(getListFail());
        });
    }
};

//修改查询条件
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//重置查询条件
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});

//按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getList(condition));
};
//分页排序搜索
export const tableSearch = page => (dispatch, getState) => {
    const query = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    if(!query.pageIndex || !page)
        query.pageIndex = 0;
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

export const SAVE_QUERY_PARTCATEGORY = 'SAVE_QUERY_PARTCATEGORY';
export const saveQueryPartCategory = data => ({
    type: SAVE_QUERY_PARTCATEGORY,
    data
});

export const SAVE_QUERY_FAULTITEM = 'SAVE_QUERY_FAULTITEM';
export const saveQueryFaultItem = data => ({
    type: SAVE_QUERY_FAULTITEM,
    data
});

export const SAVE_QUERY_FAULTMODE = 'SAVE_QUERY_FAULTMODE';
export const saveQueryFaultMode = data => ({
    type: SAVE_QUERY_FAULTMODE,
    data
});

export const SAVE_QUERY_DUTYUNIT = 'SAVE_QUERY_DUTYUNIT';
export const saveQueryDutyUnit = data => ({
    type: SAVE_QUERY_DUTYUNIT,
    data
});

export const SAVE_QUERY_DETERMINEDSUPPLIER = 'SAVE_QUERY_DETERMINEDSUPPLIER';
export const saveQueryDeterminedSupplier = data => ({
    type: SAVE_QUERY_DETERMINEDSUPPLIER,
    data
});
